package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.dto.AssetsDTO;
import com.ssm.dto.AssetsEditDTO;
import com.ssm.dto.ListDTO;
import com.ssm.entity.Chair;
import com.ssm.entity.DataBean;
import com.ssm.service.IChairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chair")
public class ChairController {

    @Autowired
    private IChairService iChairService;

    @RequestMapping("query")
    public DataBean<Chair> PageHelper(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, AssetsDTO assetsDTO) {
        //设置开启分页选项
        PageHelper.startPage(page, limit);
        List<Chair> chairs = iChairService.queryChair(assetsDTO);

        //传入获取的信息拿到分页信息
        PageInfo<Chair> pageInfo = new PageInfo<>(chairs);
        //通过分页信息获取需要的值 并设置到数据返回数据中
        DataBean<Chair> chairDataBean = new DataBean<>();
        chairDataBean.setCode(0);
        chairDataBean.setCount(pageInfo.getTotal());
        chairDataBean.setMsg("");
        chairDataBean.setData(chairs);
        return chairDataBean;
    }

    @RequestMapping("edit")
    public int editChair(AssetsEditDTO assetsEditDTO) {
        //前端传入的是财产的数据传输对象
        int result = iChairService.editChair(assetsEditDTO);

        //测试失败
        // result = 10;
        return result;
    }

    @RequestMapping("del")
    public int delChair(ListDTO chId) {
        int result = iChairService.delChair(chId);
        return result;
    }
}
